package org.tp8.sampleapp.service.facade;

import java.io.IOException;
import java.util.Collection;

import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.tp8.sampleapp.dms.model.Attachment;
import org.tp8.sampleapp.dms.model.BookDoc;
import org.tp8.sampleapp.exc.SampleAppException;
import org.tp8.sampleapp.rule.model.RuleModelWrapper;
import org.tp8.sampleapp.service.intf.BookDocService;

/**
 * The Class BookFacade.
 */
@Service("bookFacade")
public class BookDocFacade extends AbstractFacade {

	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = -2719557296439437593L;
	/**
	 * The book service.
	 */
	@Autowired
	private BookDocService bookService;

	/**
	 * Find all.
	 * 
	 * @return the collection
	 * @throws SampleAppException
	 *             the sample app exception
	 */
	public Collection<BookDoc> findAll() throws SampleAppException {
		Pageable pageable = new PageRequest(0, 100);
		Page<BookDoc> page = bookService.findAll(pageable);
		return page.getContent();
	}

	/**
	 * Save.
	 * 
	 * @param item
	 *            the teacher
	 * @return the teacher
	 * @throws SampleAppException
	 *             the sample app exception
	 */
	public String save(BookDoc item) throws SampleAppException {
		return bookService.save(item);
	}

	/**
	 * Find one.
	 * 
	 * @param id
	 *            the id
	 * @return the teacher
	 * @throws SampleAppException
	 *             the sample app exception
	 */
	public BookDoc findOne(String id) throws SampleAppException {
		ObjectId objId = ObjectId.massageToObjectId(id);
		return bookService.findOne(objId);
	}

	/**
	 * Delete.
	 * 
	 * @param id
	 *            the id
	 * @throws SampleAppException
	 *             the sample app exception
	 */
	public void delete(String id) throws SampleAppException {
		ObjectId objId = new ObjectId(id, false);
		bookService.delete(objId);
	}

	/**
	 * Validate.
	 * 
	 * @param sessionName
	 *            the session name
	 * @param entity
	 *            the entity
	 * @return the rule model wrapper
	 * @throws SampleAppException
	 *             the sample app exception
	 */
	public RuleModelWrapper validate(String sessionName, BookDoc entity)
			throws SampleAppException {
		return bookService.validate(entity);
	}

	/**
	 * Count.
	 * 
	 * @return the long
	 * @throws SampleAppException
	 *             the sample app exception
	 */
	public long count() throws SampleAppException {
		return bookService.count();
	}

	/**
	 * Fetches the attachment.
	 * 
	 * @param id
	 * @return
	 * @throws IOException
	 */
	public Attachment getAttachment(String id) throws IOException {
		ObjectId objId = ObjectId.massageToObjectId(id);
		return bookService.fetchAttachment(objId);
	}

}
